<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * 无限极分类
 * @param $data
 * @param int $pid
 * @param int $p
 * @return array
 */
function nav_array($data,$pid=0,$p=0){
    $navArray = [];
    $p++;
    foreach ($data as $k=>$v){
        if ($v->parent_id == $pid){
            if ($p != 1){
                $v->title = str_repeat('　',$p).'├── '.$v->title;
            }
            $navArray[] = $v;
            $navArray = array_merge($navArray,nav_array($data,$v->id,$p));
        }
    }
    return $navArray;
}

/**
 * 所属分类选择
 * @param $data
 * @param int $pId
 * @param int $p
 * @return array
 */
function navOption($data, $pId = 0,$p = 0)
{
    $navArray = [0=>'顶级'];
    $p++;
    foreach($data as $k => $v)
    {
        if($v['parent_id'] == $pId)
        {
            $n = $v['title'];
            if ($p != 1){
                $n = str_repeat('　',$p).'├── '.$v['title'];
            }
            $navArray+= [$v['id']=>$n];
            $navArray+= navOption($data, $v['id'],$p);
        }
    }
    return $navArray;
}
/**
 * 下级分类
 * @param $data
 * @param int $pid
 * @return array
 */
function nav_pdata($data,$pid=0){
    $navArray = [];
    foreach ($data as $k=>$v){
        if ($v['parent_id'] == $pid){
            $v['pdata'] = nav_pdata($data,$v['id']);
            $navArray[] = $v;
        }
    }
    return $navArray;
}

function nav_top($data,$pid)
{
    $navArray = [];
    foreach ($data as $key=>$value){
        # 父类
        if ($value->id == $pid){
            $navArray[] = $value;
            if ($value->parent_id != 0){
                $navArray = array_merge($navArray,nav_top($data,$value->parent_id));
            }
        }
    }
    return $navArray;
}

function status($key=null)
{
    $statusArray = [1=>'正常',2=>'隐藏',3=>'待审核',4=>'删除'];
    if (!empty($key)){
        if (isset($statusArray[$key])){
            return $statusArray[$key];
        }else{
            return '未定义属性：'.$key;
        }
    }else{
        return $statusArray;
    }

}


